#!/bin/sh
#if [ -f $DRACOSRC_PKG_SRC_DIR/logo_linux_clut224.ppm ]; then
  cp $DRACOSRC_PKG_SRC_DIR/logo_linux_clut224.ppm drivers/video/logo/ || exit 1
#fi

cp $DRACOSRC_PKG_SRC_DIR/config .config || exit 1

sed -i 's/EXTRAVERSION = '${DRACOSRC_PKG_KERNEL_PATCH}'/EXTRAVERSION = /g' Makefile

make oldconfig

if [ "${NOMAKE}" = 1 ]; then
  echo "=> Build aborted"
  exit 1
fi

make -j3 bzImage modules || exit 1

mkdir -p $DRACOSRC_PKG/{lib/modules,boot}
make INSTALL_MOD_PATH=$DRACOSRC_PKG modules_install || exit 1
cp System.map $DRACOSRC_PKG/boot/System.map-${DRACOSRC_PKG_VERSION}${DRACOSRC_PKG_VERSION_LOCAL} || exit 1
cp arch/*/boot/bzImage $DRACOSRC_PKG/boot/vmlinuz-${DRACOSRC_PKG_VERSION}${DRACOSRC_PKG_VERSION_LOCAL} || exit
cp .config $DRACOSRC_PKG/boot/config-${DRACOSRC_PKG_VERSION}${DRACOSRC_PKG_VERSION_LOCAL} || exit 1

( cd $DRACOSRC_PKG/boot ;
  ln -sf System.map-${DRACOSRC_PKG_VERSION}${DRACOSRC_PKG_VERSION_LOCAL} System.map
  ln -sf vmlinuz-${DRACOSRC_PKG_VERSION}${DRACOSRC_PKG_VERSION_LOCAL} vmlinuz
  ln -sf config-${DRACOSRC_PKG_VERSION}${DRACOSRC_PKG_VERSION_LOCAL} config
)

mkdir -p $DRACOSRC_PKG/etc/rc.d
cat $DRACOSRC_PKG_SRC_DIR/rc.modules > $DRACOSRC_PKG/etc/rc.d/rc.modules-${DRACOSRC_PKG_VERSION}${DRACOSRC_PKG_VERSION_LOCAL}.new || exit 1
chmod 755 $DRACOSRC_PKG/etc/rc.d/rc.modules-${DRACOSRC_PKG_VERSION}${DRACOSRC_PKG_VERSION_LOCAL}.new

mkdir -p $DRACOSRC_PKG/install

cat << EOF > $DRACOSRC_PKG/install/doinst.sh
config() {
  NEW="\$1"
  OLD="\$(dirname \$NEW)/\$(basename \$NEW .new)"
  # If there's no config file by that name, mv it over:
  if [ ! -r \$OLD ]; then
    mv \$NEW \$OLD
  elif [ "\$(cat \$OLD | md5sum)" = "\$(cat \$NEW | md5sum)" ]; then # toss the redundant copy
    rm \$NEW
  fi
  # Otherwise, we leave the .new copy for the admin to consider...
}

config etc/rc.d/rc.modules-${DRACOSRC_PKG_VERSION}${DRACOSRC_PKG_VERSION_LOCAL}.new

# If rc.modules is a real file, back it up:
if [ -r etc/rc.d/rc.modules -a ! -L etc/rc.d/rc.modules ]; then
  cp -a etc/rc.d/rc.modules etc/rc.d/rc.modules.bak 
fi
# Make rc.modules a symlink if it's not already, but do not replace
# an existing symlink.  You'll have to decide to point at a new version
# of this script on your own...
if [ ! -L etc/rc.d/rc.modules ]; then
  ( cd etc/rc.d ; rm -rf rc.modules )
  ( cd etc/rc.d ; ln -sf rc.modules-${DRACOSRC_PKG_VERSION}${DRACOSRC_PKG_VERSION_LOCAL} rc.modules )
fi

# A good idea whenever kernel modules are added or changed:
if [ -x sbin/depmod ]; then
  chroot . /sbin/depmod -a ${DRACOSRC_PKG_VERSION}${DRACOSRC_PKG_VERSION_LOCAL} 1> /dev/null 2> /dev/null
fi

EOF

# Use external package
#make INSTALL_HDR_PATH=${DRACOSRC_PKG}/usr headers_install || exit 1

make clean
rm .version
make oldconfig
make prepare
make scripts

mkdir tmp || exit 1
mv net/mac80211 tmp/ || exit 1
mv drivers/net/wireless tmp/ || exit 1
rm -Rf Documentation drivers fs net sound || exit 1
ls -1d arch/* | grep -v i386 | grep -v x86_64 | xargs rm -Rf || exit 1
ls -1d include/* | grep asm- | grep -v i386 | grep -v x86 | grep -v generic | xargs rm -Rf || exit 1
mkdir -p net drivers/net || exit 1
mv tmp/mac80211 net/ || exit 1
mv tmp/wireless drivers/net/ || exit 1
rm -r tmp || exit 1

cd ..

mkdir -p $DRACOSRC_PKG/usr/src
mv linux-${DRACOSRC_PKG_VERSION} $DRACOSRC_PKG/usr/src/

( cd $DRACOSRC_PKG/usr/src ; 
  mv linux-${DRACOSRC_PKG_VERSION} linux-${DRACOSRC_PKG_VERSION}${DRACOSRC_PKG_VERSION_LOCAL}
  ln -sf linux-${DRACOSRC_PKG_VERSION}${DRACOSRC_PKG_VERSION_LOCAL} linux
)

( cd $DRACOSRC_PKG/lib/modules/$DRACOSRC_PKG_VERSION${DRACOSRC_PKG_VERSION_LOCAL} ;
  ( rm -f source build;
    ln -sf ../../../usr/src/linux-$DRACOSRC_PKG_VERSION${DRACOSRC_PKG_VERSION_LOCAL} build
    ln -sf ../../../usr/src/linux-$DRACOSRC_PKG_VERSION${DRACOSRC_PKG_VERSION_LOCAL} source
  )
)
